package top.swimmer.tools.data.linked;

public class ReverseSearch {
    public static ListNode reverseSearch(ListNode head, int lastIndex) {
        if (head == null) {
            return null;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode first = dummy;
        ListNode second = dummy;

        for (int i = 0; i < lastIndex + 1; i++) {
            first = first.next;
        }

        while (first != null) {
            second = second.next;
            first = first.next;
        }

        second.next = second.next.next;
        return dummy.next;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        System.out.println(reverseSearch(node1, 1));
    }
}
